<?php

# -- DÉBUT NOTE DE LICENCE -------------------------------------
#
# Ce fichier fait partie de Clicpage.
#
# Copyright (c) 2010 Francois (www.clicpage.net)
# Distribué selon les termes de la licence GPL version 2.0.
# Consultez le fichier LICENCE ou
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- FIN NOTE DE LICENCE ---------------------------------------

class generateurLiens extends cpGenerateur
{

	
	
	
	
	/// @name Administration
	//@{
	
	
	public function preparerModule($module)
	{

		# Vérification des permissions
		if(	$this->core->auth->verifierPermission('pages') &&
			($this->page->utilisateur_id == $this->core->auth->getUtilisateurID() ||
			 $this->core->auth->verifierPermission('toutesPages') )
		)
		{
			
			# Boutons d'action
			if($this->core->auth->verifierPermission('publierPage')) {
				$module->ajouterBoutonAction(__('Publier'), '#', 'boutonAction_publier');
			}
			$module->ajouterBoutonAction(__('Enregistrer comme brouillon'), '#', 'boutonAction_enregistrer');
			
			# Actions autorisées
			$module->setActionsAutorisees('restaurerPage');
		}
		
		# Bouton d'action «Aperçu»
		$module->ajouterBoutonAction(__('Aperçu'), '#', 'boutonAction_apercu');
		
		$module->setCSS('modifierPage,modifierPage_liens');
		$module->setJavascript('modifierPage,cpConditionsPage,modifierPage_liens');
		
	}
	
	
	public function action_restaurerPage($module)
	{
		$this->page->restaurer();
		$module->messageOK(__('La page a été restaurée avec succès.'));
	}
	
	
	public function adminHead()
	{
		return
		'<script type="text/javascript" language="javascript">
			var conditionsLiens = '.$this->core->conditionsPageToJs($this->page->page_options['conditions']).';
		</script>';
	}
	
	
	public function adminBody($module)
	{

		# Valeur des champs de formulaire		
		$page_titre			= $this->page->page_titre;
		$page_entete		= $this->page->page_entete == '' ? 
			$this->page->page_entete_xhtml :
			$this->page->page_entete;
		$affiche_extrait	= $this->page->page_options['affiche_extrait'];
		$liens_par_page		= (integer) $this->page->page_options['liens_par_page'];
		$tri_champ			= $this->page->page_options['tri']['champ'];
		$tri_ordre			= $this->page->page_options['tri']['ordre'];
		$tri_libre			= $this->page->page_options['tri_libre'];

		# HTML de l'interface
		$html =
		'<form id="modifierPage"  method="post" action="index.php?module=modifierPage&page_id='.$this->page->page_id.'">'.
			form::hidden('page_id', (integer) $this->page->page_id).
			form::hidden('nonce', '').
			form::hidden('action', '').
			'<div id="modifierPage_gauche">'.
				'<div id="conteneurMessage">'.$module->afficherMessages().'</div>'.
				'<fieldset>'.
					'<legend>'.__('Liens').'</legend>'.
					'<p>'.__('Les pages qui correspondent aux critères ci-dessous seront incluses sur la page de liens.').'</p>'.
					'<div id="conditions-liens"></div>'.
					'<p>'.
						'<label for="trier_champ">'.__('Trier par').'</label>'.
						form::combo(
							'tri_champ', 											# Nom du champ
							array(													# Données
								__('Titre')					=> 'page_titre',
								__('URL')					=> 'page_url',
								__('Dossier')				=> 'cat_id',
								__('Date de création')		=> 'page_date',
								__('Date de modification')	=> 'page_date_modification'
							), 
							$tri_champ												# Valeur initiale
						).
						$module->combo(
							'tri_ordre',											# Nom du champ
							array(													# Données
								__('ordre croissant')	=> 'asc',
								__('ordre décroissant')	=> 'desc'
							), 
							$tri_ordre												# Valeur initiale
						).
					'</p>'.
					'<p>'.
						'<label for="tri_libre">'.__('Laisser le visiteur trier les résultats').'</label>'.
						$module->checkbox('tri_libre', 1, $tri_libre).
					'</p>'.
					'<p>'.
						'<label for="affiche_extrait">'.__('Afficher un extrait').'</label>'.
						$module->combo('affiche_extrait', array(__('Oui') => 1, __('Non') => 0), $affiche_extrait).
					'</p>'.
					'<p>'.
						'<label for="liens_par_page">'.__('Liens par page').'</label>'.
						$module->field('liens_par_page', 5, 5, $liens_par_page).
					'</p>'.
				'</fieldset>'.	
				'<p class="onTop">'.
					'<label for="page_titre">'.__('Titre').'</label>'.
					$module->field('page_titre', 40, 250, html::escapeHTML($page_titre)).
				'</p>'.
				'<p class="onTop">'.
					'<label for="page_entete">'.__('En-tête').'</label>'.
					$module->textArea('page_entete', 40, 20, html::escapeHTML($page_entete), 'tinymce').
				'</p>'.
			'</div>'.
			'<div id="modifierPage_droite">'.$this->adminProprietesHTML($module, array('tags')).'</div>'.
		'</form>';
		
		return $html;
		
	}
	
	
	public function curseurNouvellePage($cur)
	{
		
		parent::curseurNouvellePage($cur);
		
		$cur->page_options = array_merge($cur->page_options, array(
			'conditions'		=> array(array(1=>'and', 2=>'cat_id', 3=>'=', 4=>0),
									 array(1=>'and', 2=>'page_id', 3=>'!=', 4=>$cur->page_id)),
			'tri'				=> array('champ' => 'page_date', 'ordre' => 'desc'),
			'affiche_extrait'	=> true,
			'tri_libre'			=> false,	
			'liens_par_page'	=> 15
		));
		$cur->page_mots = implode(' ', text::splitWords(text::toUTF8(html_entity_decode(html::clean($cur->page_titre)))));
	
	}
	
	
	public function publier()
	{

		# Enregistrer la liste des médias
		$this->medias = $this->page->page_options['cp']['medias'];
		$this->adminEnregistrerMediasPage();
		
		# Mots
		$mots = html::clean($this->page->page_titre.' '.$this->page->page_entete_xhtml);
		$this->page->page_mots	= implode(' ', text::splitWords(text::toUTF8(html_entity_decode($mots))));
		
		# Publier la page
		parent::publier();
		
	}
	//@}
	
	
	
	
	/// @name Public
	//@{
	
	
	public function initPublic()
	{
		
		parent::initPublic();
		
		if($_GET[1] == 'rss.xml' || $_GET[1] == 'atom.xml')
		{
			
			$feed = new cpFeed($this->core);
			$feed->langue = $this->page->page_langue;
			$feed->titre = $this->page->page_titre.' - '.$this->core->site->getPropriete('site_titre');
			$feed->sousTitre = $this->core->site->getPropriete('site_soustitre');
			$feed->url = http::getSelfURI();
			$feed->url_source = CP_URL_PUBLIQUE.$this->page->page_url;
			$feed->description = $this->page->page_entete_xhtml;
			$feed->date_modification = 0;
			$feed->auteur = $this->page->getAuteur()->utilisateur_pseudo;
			$feed->id = 'urn:md5:'.md5($this->core->site->getPropriete('site_uid').$this->page->page_id);
			$feed->copyright = $this->core->site->getPropriete('site_copyright');
			
			$params = array(
				'page_statut'		=> CP_STATUT_ONLINE,
				'conditions_supp'	=> array($this->core->conditionsPageToSql($this->page->page_options['conditions'], $this->page->cat_id)),
				'order'				=> $this->page->page_options['tri']['champ'].' '.$this->page->page_options['tri']['ordre']
			);
			
			$params['limit'] = array(0,50);
			$pages = $this->core->site->getPages($params, CP_GET_STATIC);
			
			while($pages->fetch())
			{
				
				$tags = array();
				$rs = $pages->getTags();
				while($rs->fetch()) {
					$tags[] = $rs->tag_texte;
				}
				
				if($pages->page_entete_xhtml != '') {
					$extrait = $pages->page_entete_xhtml;
				} elseif($pages->page_contenu_xhtml != '') {
					$extrait = text::cutString(html::clean($pages->page_contenu_xhtml),300);
				} else {
					$extrait = '<p>'.__('[pas d\'extrait]').'</p>';
				}
				
				$feed->ajouterElement(array(
					'titre'				=> $pages->page_titre,
					'url' 				=> CP_URL_PUBLIQUE.$pages->page_url,
					'id' 				=> 'urn:md5:'.md5($this->core->site->getPropriete('site_uid').$pages->page_id),
					'date'				=> strtotime($pages->page_date),
					'auteur'			=> $pages->getAuteur()->utilisateur_pseudo,
					'tags'				=> $tags,
					'extrait'			=> $extrait,
					'url_commentaires' 	=> in_array($this->page->page_commentaires, array(CP_COMMENTAIRES_ACTIFS, CP_COMMENTAIRES_PERMISSION)) ?
												CP_URL_PUBLIQUE.$pages->page_url.'#formulaire-commentaire' : null
				));
				
				if(strtotime($pages->page_date) > $feed->date) {
					$feed->date = strtotime($pages->page_date);
				}
				
			}
			
			if($_GET[1] == 'atom.xml') {
				$feed->atom();
			} else {
				$feed->rss();
			}
			
		}
		
		$this->p = $_GET['page'];
		$this->p = is_numeric($this->p) ? (integer) $this->p : 1;
		
	}
	
	
	public function public_head()
	{
		if($this->page->page_options['tri_libre'])
		{
			$js =
			'<script language="javascript" type="text/javascript">
				$(document).ready(function()
				{
					$("#tri").change(function() {
						window.location = $("#url").val()+"?tri="+$(this).val();
						return false;
					});
				});
			</script>';
		}
		else
		{
			$js = '';
		}
		
		return
		$js.'<link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="'.CP_URL_PUBLIQUE_RELATIVE.$this->page->page_url.',atom.xml" />'."\n";
	}
	
	
	public function public_contenu()
	{

		# Tri
		if($this->page->page_options['tri_libre']) 
		{
			switch($_GET['tri']):
			
				case 1:
					$tri = 'page_titre ASC';
					$triId = 1;
				break;
				
				case 2:
					$tri = 'page_date ASC';
					$triId = 2;
				break;
				
				case 3:
					$tri = 'page_date DESC';
					$triId = 3;
				break;	
			
				default:
					
					$tri = $this->page->page_options['tri']['champ'].' '.$this->page->page_options['tri']['ordre'];
				
					switch ($tri):
					
						case 'page_date ASC':
							$triId = 2;
						break;
						
						case 'page_date DESC':
							$triId = 3;
						break;
						
						default:
							$triId = 1;
						break;
											
					endswitch;
					
				break;
			
			endswitch;
			
		}
		else
		{
			$tri = $this->page->page_options['tri']['champ'].' '.$this->page->page_options['tri']['ordre'];
		}
		
		
		$params = array(
			'page_statut'		=> CP_STATUT_ONLINE,
			'conditions_supp'	=> array($this->core->conditionsPageToSql($this->page->page_options['conditions'], $this->page->cat_id)),
			'order'				=> $tri
		);
		
		$count = $this->core->site->getPages($params, CP_GET_COMPTEUR);	
		
		$pager = new pager($this->p,$count->f(0),$this->page->page_options['liens_par_page'],10);
		$pager->html_prev = __('&#171;préc.');
		$pager->html_next = __('suiv.&#187;');
		$pager->var_page = 'page';
		
		$params['limit'] = array($pager->index_start,$this->page->page_options['liens_par_page']);
		$pages = $this->core->site->getPages($params, CP_GET_STATIC);
		
		if($pages->isEmpty()) {
			return '<p class="liens-aucun-resultat">'.__('Pas de page').'</p>';
		}
		
		# Avec extrait
		if($this->page->page_options['affiche_extrait'])
		{
			$html =
			'<div class="liens-liste-extrait">';
			
			while($pages->fetch())
			{
				$html.= $pages->public_apercu(true, true);				
			}
			
			$html.=
			'</div>';
			
		}
		# Sans extrait
		else
		{
			
			$html =
			'<ul class="liens-liste">';
			
			while($pages->fetch())
			{
				$html.= $pages->public_apercu(false);
			}
			
			$html.=
			'</ul>';
			
		}

		if($count->f(0) > $this->page->page_options['liens_par_page']) {
			$html.=
			'<p class="pages">'.__('Page(s) :').' '.$pager->getLinks().'</p>';
		}
		
		if($this->page->page_options['tri_libre'])
		{
			$html =
			form::hidden('url', CP_URL_PUBLIQUE_RELATIVE.$this->page->page_url).
			'<p>'.
				__('Tri').' '.
				form::combo('tri', array(
					__('Alphabétique A-Z') => 1,
					__('Du plus récent au plus ancien') => 3,
					__('Du plus ancien au plus récent') => 2
				), $triId).
			'</p>'.
			$html;
		}
		
		return $html;
		
	}
	
	
	public function affiche_tags()
	{
		return false;
	}
	//@}
	
	
}

?>